package main.java.FSMDE.persistence;

import java.util.ArrayList;
import java.util.UUID;

import main.java.FSMDE.models.Diagram;
import main.java.FSMDE.models.InterfaceCollection;
import main.java.FSMDE.models.TransitionCollection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


public class DiagramXmlManager implements IXmlManager<Diagram> {

	private static String fsmType = "fsm";
	private ArrayList<main.java.ERMDE.models.Diagram> ermDiagrams;

	public Element getElementFromItem(Diagram diagram, Document document) {
		Element diagramElement = document.createElement("diagram");

		diagramElement.setAttribute("name", diagram.getName());
		diagramElement.setAttribute("id", diagram.getId().toString());
		diagramElement.setAttribute("type", fsmType);

		diagramElement.appendChild(new InterfaceCollectionXmlManager()
				.getElementFromItem(diagram.getInterfaces(), document));
		diagramElement.appendChild(new TransitionCollectionXmlManager(
				new InterfaceCollection()).getElementFromItem(
				diagram.getTransitions(), document));

		Element element = document.createElement("subDiagrams");
		for (Diagram subDiagram : diagram.getSubDiagrams()) {
			element.appendChild(this.getSubDiagramElement(subDiagram, document));
		}
		diagramElement.appendChild(element);

		return diagramElement;
	}

	public Element getSubDiagramElement(Diagram diagram, Document document) {
		Element subDiagramElement = document.createElement("diagram");

		subDiagramElement.setAttribute("id", diagram.getId().toString());
		subDiagramElement.setAttribute("name", diagram.getName());

		return subDiagramElement;
	}

	public Diagram getItemFromXmlElement(Element diagramElement)
			throws Exception {
		if (ermDiagrams == null) {
			ermDiagrams = new ArrayList<main.java.ERMDE.models.Diagram>();
		}
		if (!diagramElement.getAttribute("type").equalsIgnoreCase(
				DiagramXmlManager.fsmType)) {
			return null;
		}
		Diagram diagram = new Diagram();
		String name = diagramElement.getAttribute("name");
		UUID id = UUID.fromString(diagramElement.getAttribute("id"));
		diagram.setId(id);
		diagram.setName(name);

		Element infacesElem = (Element) diagramElement.getElementsByTagName(
				"interfaces").item(0);
		InterfaceCollectionXmlManager interfaceCollectionXmlManager = new InterfaceCollectionXmlManager();
		interfaceCollectionXmlManager.setErmDiagrams(ermDiagrams);
		InterfaceCollection infaceColl = interfaceCollectionXmlManager
				.getItemFromXmlElement(infacesElem);
		diagram.setInterfaces(infaceColl);

		Element transitionsElem = (Element) diagramElement
				.getElementsByTagName("transitions").item(0);
		TransitionCollectionXmlManager transitionCollectionManager = new TransitionCollectionXmlManager(
				infaceColl);
		TransitionCollection transitionColl = transitionCollectionManager
				.getItemFromXmlElement(transitionsElem);
		diagram.setTransitions(transitionColl);

		new ErmFsmRelationXmlManager().generateRelations(diagram, infacesElem,
				ermDiagrams);

		Element subdiagramsElement = (Element) diagramElement
				.getElementsByTagName("subDiagrams").item(0);
		if (subdiagramsElement == null) {
			return diagram;
		}

		for (int i = 0; i < subdiagramsElement.getChildNodes().getLength(); i++) {
			Node node = subdiagramsElement.getChildNodes().item(i);
			if (node == null || !(node instanceof Element)) {
				continue;
			}
			diagram.getSubDiagramNames().add(
					this.getSubDiagramFromXmlElement((Element) node));
		}

		return diagram;
	}

	public String getSubDiagramFromXmlElement(Element diagramElement) {
		String subdiagramName = diagramElement.getAttribute("name");
		return subdiagramName;
	}

	public void setErmDiagramas(ArrayList<main.java.ERMDE.models.Diagram> ermDiagrams) {
		this.ermDiagrams = ermDiagrams;
	}

}
